package com.smartstate.autoMaticControl.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartstate.autoMaticControl.constant.DeviceTypeEnum;
import com.smartstate.autoMaticControl.entity.ColdSite;
import com.smartstate.autoMaticControl.entity.Dedusting;
import com.smartstate.autoMaticControl.service.EquipmentBoardService;
import com.smartstate.autoMaticControl.vo.*;
import com.smartstate.autoMaticControl.websocket.WebSocketServer;
import com.smartstate.common.web.exception.CommonException;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;


@Service
public class EquipmentBoardServiceImpl implements EquipmentBoardService {

    @Resource
    private RedisTemplate redisTemplate;

    private  String deviceId;

    private  String userId;

    private  String deviceType;

    private HashSet<String> set=new HashSet<>();

    public String getDeviceId(){
        return deviceId;
    }

    public void setDeviceId(String deviceId){
        this.deviceId = deviceId;
    }

    public String getUserId(){
        return userId;
    }

    public void setUserId(String userId){
        this.userId = userId;
    }

    public String getDeviceType(){
        return deviceType;
    }

    public void setDeviceType(String deviceType){
        this.deviceType = deviceType;
    }


    @Async("genericExecutor")
    //@Scheduled(cron = "0/1 * * * * ?")
    @Override
    public void coldSite() {
        HashMap<String, Object> map = new HashMap<>();
        if (redisTemplate.opsForHash().entries("DEVICETYPE_"+DeviceTypeEnum.COLD_SITE_TYPE.getValue()+"_"+"UserID").isEmpty()){
            throw new CommonException("redis无冷站数据");
        }else {
            userId = (String) redisTemplate.opsForHash().entries("DEVICETYPE_" + DeviceTypeEnum.COLD_SITE_TYPE.getValue() + "_" + "UserID").get("userId");
            deviceId = (String) redisTemplate.opsForHash().entries("DEVICETYPE_" + DeviceTypeEnum.COLD_SITE_TYPE.getValue() + "_" + "UserID").get("deviceId");
            deviceType = (String) redisTemplate.opsForHash().entries("DEVICETYPE_" + DeviceTypeEnum.COLD_SITE_TYPE.getValue() + "_" + "UserID").get("deviceType");
        }
        set.add(deviceType);
        //if (WebSocketServer.webSocketMap.get(userId)==null){
        //    throw new CommonException("无连接，等待连接");
        //}
        Object coldSiteObj = redisTemplate.opsForHash().entries(this.deviceId);
        ObjectMapper objectMapper = new ObjectMapper();
        ColdSite coldSiteDate = objectMapper.convertValue(coldSiteObj, ColdSite.class);
        if (coldSiteDate.getType()=="0"){
            throw new CommonException("设备异常离线");
        }else {
            map.put(deviceId, coldSiteDate);
            //
            // BackwaterVO backwaterVO = new BackwaterVO();//回水设备
            // BeanUtils.copyProperties(coldSiteDate, backwaterVO);
            //
            // OutWaterVO outWaterVO = new OutWaterVO();//出水设备
            // BeanUtils.copyProperties(coldSiteDate,outWaterVO);
            //
            // CompressorVO compressorVO = new CompressorVO();//压缩机
            // BeanUtils.copyProperties(coldSiteDate,compressorVO);
            //
            // CondenserVO condenserVO = new CondenserVO();//冷凝
            // BeanUtils.copyProperties(coldSiteDate,condenserVO);
            //
            // CoolingTowerVO coolingTowerVO = new CoolingTowerVO();//冷却塔
            // BeanUtils.copyProperties(coldSiteDate,coolingTowerVO);
            //
            // EvaporatorVO evaporatorVO = new EvaporatorVO();//蒸发器
            // BeanUtils.copyProperties(coldSiteDate,evaporatorVO);
            //
            // MotorVO motorVO = new MotorVO();//马达
            // BeanUtils.copyProperties(coldSiteDate,motorVO);
            //
            // OtherVO otherVO = new OtherVO();//其他数据
            // BeanUtils.copyProperties(coldSiteDate,otherVO);
            //
            // WaterPumpVO waterPumpVO = new WaterPumpVO();//水泵
            // BeanUtils.copyProperties(coldSiteDate,waterPumpVO);
            // map.put("水泵",waterPumpVO);
            // map.put("其他信息",otherVO);
            // map.put("马达",motorVO);
            // map.put("蒸发器",evaporatorVO);
            // map.put("冷却塔",coolingTowerVO);
            // map.put("冷凝",condenserVO);
            // map.put("压缩机",compressorVO);
            // map.put("出水",outWaterVO);
            // map.put("回水",backwaterVO);
            // map.put("胶球清洗装置",coldSiteDate.getJellyStatus());
            // map.put("加药装置",coldSiteDate.getDosingStatus());
            // map.put("补水装置",coldSiteDate.getMoisturizingStatus());
            // map.put("阀门",coldSiteDate.getValveOpening());
            //try {
            //    //WebSocketServer.sendInfo(coldSiteDate,);
            //} catch (IOException e) {
            //    throw new CommonException("冷站数据推送失败");
            //}
        }
    }

    /**
     * redis冷站实时假数据上传
     */
    //@Async("scorePoolTaskExecutor")
    //@Scheduled(cron = "0/1 * * * * ?")
    @Override
    public void redisDe() {
        if(StringUtils.isNotEmpty(deviceId) && getResult(DeviceTypeEnum.COLD_SITE_TYPE.getValue().toString())){
            ColdSite coldSite = new ColdSite();
            coldSite.setOwTemp(RandomStringUtils.randomAscii(4));
            coldSite.setPumpStatus(RandomStringUtils.randomAscii(4));
            coldSite.setValveOpening(RandomStringUtils.randomAscii(4));
            coldSite.setOwPressure(RandomStringUtils.randomAscii(4));
            coldSite.setAscentFlow(RandomStringUtils.randomAscii(4));
            coldSite.setPumpLoad(RandomStringUtils.randomAscii(4));
            coldSite.setPumpFrequency(RandomStringUtils.randomAscii(4));
            coldSite.setWaterTempLN(RandomStringUtils.randomAscii(4));
            coldSite.setInletTempLN(RandomStringUtils.randomAscii(4));
            coldSite.setCoalPressureLN(RandomStringUtils.randomAscii(4));
            coldSite.setCoalTempLN(RandomStringUtils.randomAscii(4));
            coldSite.setOilPressureYS(RandomStringUtils.randomAscii(4));
            coldSite.setOilTempYS(RandomStringUtils.randomAscii(4));
            coldSite.setGvOpeningYS(RandomStringUtils.randomAscii(4));
            coldSite.setElectricity(RandomStringUtils.randomAscii(4));
            coldSite.setEleFeedback(RandomStringUtils.randomAscii(4));
            coldSite.setRunTime(RandomStringUtils.randomAscii(4));
            coldSite.setWaterTempZF(RandomStringUtils.randomAscii(4));
            coldSite.setInletTempZF(RandomStringUtils.randomAscii(4));
            coldSite.setCoalPressureZF(RandomStringUtils.randomAscii(4));
            coldSite.setCoalTempZF(RandomStringUtils.randomAscii(4));
            coldSite.setCoilTemp(RandomStringUtils.randomAscii(4));
            coldSite.setBearingTemp(RandomStringUtils.randomAscii(4));
            coldSite.setGasTemp(RandomStringUtils.randomAscii(4));
            coldSite.setRwTemp(RandomStringUtils.randomAscii(4));
            coldSite.setRwPressure(RandomStringUtils.randomAscii(4));
            coldSite.setJellyStatus(RandomStringUtils.randomAscii(4));
            coldSite.setChillingStatus(RandomStringUtils.randomAscii(4));
            coldSite.setChillingLoad(RandomStringUtils.randomAscii(4));
            coldSite.setChillingFrequency(RandomStringUtils.randomAscii(4));
            coldSite.setDosingStatus(RandomStringUtils.randomAscii(4));
            coldSite.setMoisturizingStatus(RandomStringUtils.randomAscii(4));
            Map<String, Object> map = com.baomidou.mybatisplus.core.toolkit.BeanUtils.beanToMap(coldSite);
            redisTemplate.opsForHash().putAll(deviceId,map);
        }else {
            throw new CommonException("冷站设备详情数据无,稍后再试");
        }
    }

    /**
     * 判断redis是否存在type
     * @param type
     * @return
     */
    public boolean getResult(String type){
        for (String deviceId : set) {
            if (type.equals(deviceId)){
                return true;
            }
        }
        return false;
    }

}
